<template>
  <view class="settle-container">
    <!--全选商品-->
    <label class="radio" @click="changeAllState">
      <radio color="#C00000" :checked="isAllChecked" /><text>全选</text>
    </label>
    <!--合計金额-->
    <view class="total-price-amount">
      合計:<text class="total-price">{{checkedTotalPrice}}元</text>
    </view>

    
    <!--结算-->
    <view class="settle-button" @click="settlement">
      结算({{checkedCount}})
    </view>
    
    
  </view>
</template>

<script>
  import {mapGetters,mapMutations, mapState} from 'vuex'
  export default {
    name:"settle-bar",
    data() {
      return {
        //倒計時秒數
        seconds:3,
        //定時器
        timer:null
      };
    },
    methods:{
      ...mapMutations('shopCar',['stateAllChanged']),
      ...mapMutations('user',['updateRedirectInfo']),
      changeAllState(){
        this.stateAllChanged(!this.isAllChecked)
      },
      //結算
      settlement(){
        if(!this.checkedCount) return uni.$showMessage('請选择商品')
        if(!this.addressStr)return uni.$showMessage('請选择地址')
        if(!this.token)return this.delayNavigate()
        this.payOrder()
      },
      delayNavigate(){
        this.seconds = 3
        this.showLoginTips(this.seconds)
        this.timer = setInterval(() =>{
          this.seconds--
          if(this.seconds <= 0){
            clearInterval(this.timer)
            uni.switchTab({
              url:'/pages/mine/mine',
              success:() =>{
                this.updateRedirectInfo({
                  openType:'switchTab',
                  from:'/page/shopCar/shopCar'
                })
              }
            })
            return
          }
          this.showLoginTips(this.seconds)
        },1000)
      },
      //跳轉登入页的提示
      showLoginTips(n){
        uni.showToast({
          icon:'none',
          title:'請登入后再结算!' + n,
          mask:true,
          duration: 1500
        })
      },
      //支付
      async payOrder(){
        //创建訂單,先组织訂單对單
        const orderInfo = {
          order_price: 0.01,
          consignee_addr:this.addressStr,
          products:this.shopCar.filter(x => x.product_state).map(
                x =>({product_id:x.product_id,
                product_number:x.product_count,
                product_price:x.product_price}))
        }
        //發起請求創建訂單
        const{data:res} = await uni.$http.post('/mine/shop/create/order', orderInfo)
        if(res.meta.status !== 200) return uni.$showMessage('创建訂单失败')
        //得到服務器訂單編號
        const orderNumber = res.message.order_number
        //预支付，發起請求獲得訂單支付信息
        const{data:res2} = await uni.$http.post('/mine/shop/pay/order',{orderNumber:orderNumber})
        if(res2.meta.status!==200)return uni.$showMessage('预支付訂單生成失敗')
        //得到支付相關參數
        const payInfo = res2.message.pay
        console.log(payInfo)
        //發起微信支付
        const [err, succ] = await uni.requestPayment(payInfo)
        if(err) return uni.$showMessage('訂单未支付')
        //檢測支付完成
      }
    },
    computed:{
      ...mapGetters('shopCar',['checkedCount','total','checkedTotalPrice']),
      ...mapGetters('user',['addressStr']),
      ...mapState('user',['token']),
      ...mapState('shopCar',['shopCar']),
      //檢查是否全选
      isAllChecked(){
        return this.total === this.checkedCount
      },
    }
  }
</script>

<style lang="scss">
.settle-container{
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  background-color: #4CD964;
  display: flex;
  justify-content: space-around;
  align-items: center;
  font-size: 14px;
  padding-left: 5px;
  .radio{
    display: flex;
    align-items: center;
  }
  
  .total-price-amount{
    .total-price{
      color: #E43D33;
      font-weight: bold;
    }
  }
  .settle-button{
    background-color: #F0AD4E;
    height: 50px;
    line-height: 50px;
    padding: 0 10px;
    min-width: 100px;
    text-align: center;
  }
}
</style>
